#include "HandAnalyser.h"
//#include "interface.h"

xn::Context		  g_Context;
XnVSessionManager *g_pSessionManager = NULL;
XnVSwipeDetector  *g_pSwipe          = NULL;

HandAnalyser *g_pHand = new HandAnalyser;

void XN_CALLBACK_TYPE SessionStart(const XnPoint3D& ptFocus, void *pUserCxt)
{
	cout << "SessionStart" << endl;
}

void XN_CALLBACK_TYPE SessionEnd(void *pUserCxt)
{
	cout << "SessionEnd" << endl;
}

void XN_CALLBACK_TYPE SwipeLeft(XnFloat fVelocity, XnFloat fAngle, void* pUserCxt)
{
	cout << "Swipe Left, " << "Velocity = " << fVelocity << endl;
}

void XN_CALLBACK_TYPE SwipeRight(XnFloat fVelocity, XnFloat fAngle, void* pUserCxt)
{
	cout << "Swipe Right, " << "Velocity = " << fVelocity << endl;
}

void XN_CALLBACK_TYPE SwipeUp(XnFloat fVelocity, XnFloat fAngle, void* pUserCxt)
{
	cout << "Swipe Up, " << "Velocity = " << fVelocity << endl;
}

void XN_CALLBACK_TYPE SwipeDown(XnFloat fVelocity, XnFloat fAngle, void* pUserCxt)
{
	cout << "Swipe Down, " << "Velocity = " << fVelocity << endl;
}

void XN_CALLBACK_TYPE SwipePointUpdate(const XnVHandPointContext* pContext, void* cxt)
{
	if ((g_pHand->getHandPointNum()) == HandAnalyser::m_frameNum)
	{
		cout << "**********************5 frames*******************" << endl;
		cout << "Gesture Direction(1 left, 2 right, 3 Updown, 4 Unknown): " << g_pHand->getHandDirection() << endl;
		g_pHand->printHandPoint();
		g_pHand->WritetoTestSet();
		g_pHand->WritetoTrainingSet();
		g_pHand->releaseFirstHandPoint();
		cout << "After Release First Hand Point" << endl;
		g_pHand->printHandPoint();
	}
	g_pHand->loadHandPoint(pContext);
}


#define SAMPLE_XML_FILE "C:/Program Files/Prime Sense/NITE/Data/Sample-Tracking.xml"

int main(int argc, char** argv)
{
	// Configure
	XnStatus rc = g_Context.InitFromXmlFile(SAMPLE_XML_FILE);
	if (rc != XN_STATUS_OK)
	{
		printf("Couldn't initialize from file: %s\n", xnGetStatusString(rc));
		return 1;
	}
	
	g_pSessionManager = new XnVSessionManager();
	rc = g_pSessionManager->Initialize(&g_Context, "Wave,Click", "RaiseHand");
	if (rc != XN_STATUS_OK)
	{
		printf("Couldn't initialize the Session Manager: %s\n", xnGetStatusString(rc));
		return 1;
	}

	g_pSessionManager->RegisterSession(NULL, &SessionStart, &SessionEnd);

	g_pSwipe = new XnVSwipeDetector;
	g_pSwipe->RegisterSwipeLeft(NULL, &SwipeLeft);
	g_pSwipe->RegisterSwipeRight(NULL, &SwipeRight);
	g_pSwipe->RegisterSwipeUp(NULL, &SwipeUp);
	g_pSwipe->RegisterSwipeDown(NULL, &SwipeDown);
	g_pSwipe->RegisterPointUpdate(NULL, &SwipePointUpdate);
	
	g_pSessionManager->AddListener(g_pSwipe);
	g_Context.StartGeneratingAll();
	
	while (true)
	{
		//cout <<	"Frame " << frame << endl;
		g_Context.WaitAndUpdateAll();
		// Process the data
		g_pSessionManager->Update(&g_Context);
	}
}